package com.usdt.web.controller.cp.activity;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.usdt.common.core.domain.R;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;


@RestController
@Slf4j
@RequestMapping("/test")
@Tag(name = "test")
public class TesttController {
	
    @Autowired
    private RestTemplate restTemplate;
    
	//cp
	public static final String FIND_USER = "/api/open/find/user/pageInfo";
	public static final String SAVE_OR_UPDATE_USER = "/api/open/user/saveOrUpdateBatch";
	public static final String FIND_USER_BY_ID = "/api/open/find/userById?userId=";
	public static final String SAVE_OR_UPDATE_USER_BY_EMAIL = "/api/open/user/saveOrUpdateBatchByEmail";
	public static final String EXTERNAL_LOGIN = "/api/open/external/login"; 
	
	//@Value("${lowcode.accessKeyId}")
	private String accessKeyId="E4gSGVZyDmDgttdP";
	
	//@Value("${lowcode.accessKeySecret}")
	private String accessKeySecret="iDtRCQoriW1Fxjp1TWlKnf8UO0Sy8y";
	//@Value("${lowcode.basepath}")
	
	private String BASEPATH="https://ipaas.lowcode.fosun.com";
	
	//@Value("${lowcode.accessUserId}")
	//private String accessUserId;
	
	
	//填报人
	
    @GetMapping("/test/{id}")
    @Operation(summary ="详情")
    public R<String> insert(@Schema(description = "活动模板id")@PathVariable(value = "id", required = true) String id) {
    	getCpUserByUserId(id);
    	return R.ok();
    }
	
	private <T> T postHttpResut(String path, String params,Class<T> responseType) {
		//请求头
		HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.set("Connection", "Keep-Alive");
		headers.add("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36");
		
		String timestamp = Calendar.getInstance().getTimeInMillis() + "";
        String sign = this.md5(accessKeyId + accessKeySecret + timestamp);
        headers.set("accessKeyId", accessKeyId);
        headers.set("sign", sign);
        headers.set("timestamp", timestamp);
        
        HttpEntity<String> request = new HttpEntity<>(params, headers);
        return  restTemplate.postForObject(BASEPATH+path, request, responseType);
	}
	
    private String md5(String plainText) {
        byte[] secretBytes = null;
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(plainText.getBytes());
            secretBytes = md.digest();
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException("没有md5这个算法！");
        }
        String md5code = new BigInteger(1, secretBytes).toString(16);
        int length = md5code.length();
        for (int i = 0; i < 32 - length; i++) {
            md5code = "0" + md5code;
        }
        return md5code;
    }

	public Map<String, Object> getCpUserByUserId(String userId) {
		String resp = this.postHttpResut(FIND_USER_BY_ID+userId,JSON.toJSONString(new HashMap<String, Object>()),String.class);
		if(null == resp) {
		}
		JSONObject userInfo = JSON.parseObject(resp);
		return null;
	}
}
